use super::*;

pub async fn write_training_to_mdbook(training: Training, out: PathBuf) -> eyre::Result<()> {
    static BOOK_INFO: &str = include_str!("book.toml");
    let training = training.training;

    let problems = fetch_problems(&training).await?;

    fs::create_dir_all(&out)?;
    let book_info = out.join("book.toml");
    fs::write(book_info, BOOK_INFO)?;
    let src = out.join("src");
    fs::create_dir_all(&src)?;
    let summary_path = src.join("SUMMARY.md");
    let mut summary = String::with_capacity(training.len() * 10);
    summary.push_str("# Summary\n\n");
    for (problem, md) in training.into_iter().zip(problems.into_iter()) {
        summary.push_str(&format!("- [{0}](./{0}.md)\n", &problem));
        let md = format!("{}\n{}", include_str!("script.html"), md);
        let problem_path = src.join(format!("{}.md", &problem));
        fs::write(problem_path, md)?;
    }
    fs::write(summary_path, summary)?;
    Ok(())
}
